<cfcomponent>
	<cffunction name="upload" access="public" returntype="struct" hint="Uploads an image to the images folder.  Will return structure with image name and if thumbnails was set to yes additional info will be in the structure">
		<cfargument name="fileField" required="yes" type="string" hint="name of form field in which the file is being posted">
		<cfargument name="dir" required="no" type="string" default="/" hint="Directory within the images folder to upload this to">
        <cfargument name="uploadpath" required="no" type="string" default="#request.basepath#images/" hint="full path to the images folder on the server">
		<cfargument name="createthumbs" required="no" type="string" default="no" hint="set to yes if you want thumbnails to be generated for the uploaded image">
        
        <cffile action="upload" filefield="#fileField#" destination="#uploadpath##dir#" nameconflict="makeunique" accept = "image/gif, image/pjpeg, image/jpg, image/jpeg, image/png" mode="777">
		<!---remove special characters from the file name because some things just don't work right (like cfimage) with certain characters in the name--->
		<cfset thefile = cffile.serverfile>
        <cfset thefile = replace(thefile, " ", "", "ALL")>
        <cfset thefile = replace(thefile, "&", "", "ALL")>
        <cfset thefile = replace(thefile, "_", "", "ALL")>
        <cfset thefile = replace(thefile, "(", "", "ALL")>
        <cfset thefile = replace(thefile, ")", "", "ALL")>
        <cfset thefile = replace(thefile, "?", "", "ALL")>
        <cfset thefile = replace(thefile, "<", "", "ALL")>
        <cfset thefile = replace(thefile, ">", "", "ALL")>
        <cfset thefile = replace(thefile, "@", "", "ALL")>
        <cfset thefile = replace(thefile, "!", "", "ALL")>
        <cfset thefile = replace(thefile, "*", "", "ALL")>
        <cfset thefile = replace(thefile, "^", "", "ALL")>
        <cfset thefile = replace(thefile, "%", "", "ALL")>
        
        <cfset fullDespath = "#uploadpath##dir##thefile#">
        <cfset fullDespath = replacenocase(FullDesPath, "//", "/", "ALL")>
        
        <cfset fullFrompath = "#uploadpath##dir##cffile.serverfile#">
        <cfset fullFrompath = replacenocase(FullFromPath, "//", "/", "ALL")>
        
        <cfif NOT thefile IS cffile.serverfile>
            <cffile action = "rename"  source = "#fullfrompath#" destination = "#fulldespath#">
        </cfif>        
		
        <cfif createthumbs IS 'yes'>
        	<cfinvoke method="thumbnails" sourceimage="#uploadpath##dir##thefile#" imagespath="#uploadpath#" returnvariable="imageinfo">
        </cfif>

        <cfset imageinfo.filename = thefile>

		<cfreturn imageinfo>
    </cffunction>

	<cffunction name="thumbnails" access="public" returntype="struct" hint="generate thumbnails for an image, returns width and height for the generated images.">
		<cfargument name="proc" required="no" type="string" default="cf8image" hint="Name of the processor you want to use to generate the thumbnails">
        <cfargument name="sourceimage" required="yes" type="string" hint="filename of the image">
        <cfargument name="sourcepath" required="yes" type="string" hint="Full actual path to where the sourceimage is">
        <cfargument name="imagespath" required="yes" type="string" hint="Full actual path to where the folders are (large, medium, small, tiny)">
		<cfargument name="maxwidth" required="no" type="string" default="800" hint="Maximum width of the largest image">
        <cfargument name="mwidth" required="no" type="string" default="480" hint="Max width of medium file">
        <cfargument name="swidth" required="no" type="string" default="150" hint="Max width of small file">
        <cfargument name="twidth" required="no" type="string" default="75" hint="Max width of tiny file">                 
		
		<cfif proc is 'cf8image'>
        
            <!---resize the image and create thumbnails--->
            <cfimage action = "info" source="#sourcepath##sourceimage#" structName="imginfo">

            <!---Large Image--->
            <cfif imginfo.width GT #maxwidth#>
                <cfimage action="resize"
                source="#sourcepath##sourceimage#"
                width="#maxwidth#"
                height=""
                destination="#imagespath#large/#sourceimage#"
                overwrite="yes"> 
            <cfelse>
                <!---do nothing since the large image is reasonable size--->
            </cfif>
            
            <!---Medium Image--->
            <cfif imginfo.width GT #mwidth#>
                <cfimage action="resize"
                source="#sourcepath##sourceimage#"
                width="#mwidth#"
                height=""
                destination="#imagespath#medium/#sourceimage#"
                overwrite="yes"> 
            <cfelse><!---Just copy it--->
                <cffile action = "copy" source="#sourcepath##sourceimage#" destination="#imagespath#medium/#sourceimage#" nameconflict="overwrite">
            </cfif>
        
            <!---Thumbnail--->	
            <cfif imginfo.width GT #swidth#>
                <cfimage action="resize"
                source="#sourcepath##sourceimage#"
                width="#swidth#"
                height=""
                destination="#imagespath#small/#sourceimage#"
                overwrite="yes"> 
            <cfelse><!---Just copy it--->
                <cffile action = "copy" source="#sourcepath##sourceimage#" destination="#imagespath#small/#sourceimage#" nameconflict="overwrite">
            </cfif>
        
            <!---tiny Thumnail--->	
            <cfif imginfo.width GT #twidth#>
                <cfimage action="resize"
                source="#sourcepath##sourceimage#"
                width="#twidth#"
                height=""
                destination="#imagespath#tiny/#sourceimage#"
                overwrite="yes"> 
            <cfelse><!---Just copy it--->
                <cffile action = "copy" source="#sourcepath##sourceimage#" destination="#imagespath#tiny/#sourceimage#" nameconflict="overwrite">
            </cfif>
            
            <!---Now read all three so that the height and width can be stored in the db for future use--->
            <cfimage Action = "info" source="#imagespath#large/#sourceimage#" structName="imginfo">
                <cfset iHeight = #imginfo.HEIGHT#>
                <cfset iWidth = #imginfo.WIDTH#>
                    
            <cfimage Action = "info" source="#imagespath#medium/#sourceimage#" structName="imginfo">
                <cfset mHeight = #imginfo.HEIGHT#>
                <cfset mWidth = #imginfo.WIDTH#>
                
            <cfimage Action = "info" source="#imagespath#small/#sourceimage#" structName="imginfo">
                <cfset sHeight = #imginfo.HEIGHT#>
                <cfset sWidth = #imginfo.WIDTH#>
            
            <cfimage Action = "info" source="#imagespath#tiny/#sourceimage#" structName="imginfo">
                <cfset tHeight = #imginfo.HEIGHT#>
                <cfset tWidth = #imginfo.WIDTH#>
		</cfif>

		<cfset imageinfo.iwidth = iWidth>
		<cfset imageinfo.iheight = iHeight>
		<cfset imageinfo.mwidth = mWidth>
		<cfset imageinfo.mheight = mHeight>
		<cfset imageinfo.swidth = sWidth>
		<cfset imageinfo.sheight = sHeight>
		<cfset imageinfo.twidth = tWidth>
		<cfset imageinfo.theight = tHeight>
		<cfreturn imageinfo>
	</cffunction>
</cfcomponent>